package com.order.model.sys;

import java.util.List;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Model;

/** 
* @ClassName: SystemModel 
* @Description: 后台系统模块
* @author LinBo
* @date 2014年3月22日 下午6:39:04 
*  
*/ 

@TableBind(tableName="sys_module", pkName="id")
public class SysModule extends Model<SysModule> {

	private static final long serialVersionUID = -2684054407528689803L;
	public static final SysModule dao = new SysModule();
	
	/**加载系统模块，跟据用户拥有的子菜单来决定*/
	public List<SysModule> getModule(int userId) {
		String sql = " select distinct module.name, module.id, module.sq "
				+ " from sys_user_role ur, sys_role_menu rm,sys_menu menu, sys_module module "
				+ " where menu.sys_module_id = module.id "
				+ " and menu.id = rm.sys_menu_id  "
				+ " and ur.sys_role_id = rm.sys_role_id "
				+ " and ur.sys_user_id = ? ";
		List<SysModule> parentModule =  dao.find(sql, userId);
		System.out.println(parentModule);
		return parentModule;
	}

	
	/** 
	* @Title: getJsonTree 
	* @描述: 拼接成json
	* @param @return   
	* @return String    
	* @throws 
	*/ 
	public String getJsonTree() {
		StringBuffer json = new StringBuffer("[{\"moduleid\":\"0\",\"text\":\"所有模块\",\"children\":["); // 初始化根节点
		String sql = "select m.id, m.name, m.sq from sys_module m order by m.sq desc";
		List<SysModule> module = SysModule.dao.find(sql);
		StringBuffer parent_json = new StringBuffer();
		if (module != null && module.size() > 0) {
			for (int i = 0; i < module.size(); i++) {
				int moduleId = module.get(i).getNumber("ID").intValue();
				parent_json.append("{\"moduleid\":\""+ moduleId + "\",");
				parent_json.append("\"text\":\""+ module.get(i).getStr("NAME").toString().trim()+ "\",");
				parent_json.append("\"sq\":\""+ module.get(i).getNumber("SQ").intValue()+ "\"");
				parent_json.append(SysMenu.dao.getMenuByModuleId(moduleId)); // 下级节点
			}
		} else {
			parent_json.append("},");
		}
		json.append(parent_json.toString());
		json = new StringBuffer(json.substring(0, json.length() - 1) + "]}]");
		return json.toString();
	}
}
